package com.ymatou.iapi.accounting.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ymatou.iapi.accounting.parameter.AccountBean;
import com.ymt.base.YmatouBaseCall;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class GetAccountByUserIdCall extends YmatouBaseCall {

    private static String opurl = "api/Account/GetAccountByUserId";

    public GetAccountByUserIdCall() {
        super("http://accounting.iapi.ymatou.com/", opurl, "POST", "JSON");
    }

    @Override
    public void addDefHeader() {
        client.addHeader("Accept",
                "text/xml,text/javascript,text/html,application/json");
        client.addHeader("Content-Type", "application/json; charset=UTF-8");
    }

    // 获取 accountid
    public String getAccountId() throws JSONException {
        String cid = null;
        cid = new JSONObject(getReturnData()).getJSONObject("Data").getString(
                "AccountId");
        return cid;
    }

    // get data
    public String getData() throws JSONException {
        String data = null;
        data = new JSONObject(getReturnData()).getString("Data");
        return data;
    }

    // get the return account info list
    public AccountBean getReturnAccount() throws JSONException {
        AccountBean account = null;
        String data = getData();
        if (data != null) {
            Gson gson = new Gson();
            account = gson.fromJson(data, AccountBean.class);
        }
        return account;
    }

    // get the return account info list
    public ArrayList<AccountBean> getReturnAccountList() throws JSONException {
        ArrayList<AccountBean> list = new ArrayList<AccountBean>();
        String data = getData();
        if (data != null) {
            Gson gson = new Gson();
            list = gson.fromJson(data, new TypeToken<ArrayList<AccountBean>>() {
            }.getType());
        }
        return list;
    }
}
